package com.xiaose.apt.code.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.xiaose.apt.core.entity.BaseEntity;

import java.util.Collections;

/**
 * @className: CodeGenerator
 * @Author: liuzheng
 * @Date: 2021/12/9 16:12
 * @Description:
 */
public class CodeGenerator {

    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/apt?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8", "root", "123456")
                .globalConfig(builder -> {
                    builder.author("liuzheng") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件

                            .outputDir(projectPath+ "/src/main/java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.xiaose.apt") // 设置父包名
                            .moduleName("user.login") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, projectPath+ "/src/main/resources/mapper/")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("sm_user_login") // 设置需要生成的表名
                            .addTablePrefix("sm_"); // 设置过滤表前缀
                })
                .strategyConfig((scanner, builder) -> builder.addInclude("sm_user_login") // 设置需要生成的表名
                        .addTablePrefix("sm_") // 设置过滤表前缀
                        .controllerBuilder().enableRestStyle().enableHyphenStyle()
                        .entityBuilder().enableLombok().addTableFills(
                                new Column("create_time", FieldFill.INSERT),
                                new Column("update_time", FieldFill.INSERT_UPDATE)
                        ).logicDeleteColumnName("delete_flag").logicDeletePropertyName("deleteFlag").naming(NamingStrategy.underline_to_camel)
                        .superClass(BaseEntity.class)
                        .addSuperEntityColumns("id","create_time","update_time","delete_flag")
                        .build())
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
